package com.mp3s.strategy_a;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.mp3s.interface3.BSData;
import com.mp3s.interface3.IBS;
import com.mp3s.interface3.IStrategy;
import com.mp3s.interface3.QuoteData;
import com.mp3s.mail3.MailImpl;
import com.mp3s.strategy_a.utils.BS;
import com.mp3s.strategy_a.utils.schedule;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;


public class strategyA implements IStrategy,IBS{

	private String version="strategyA-0.0.1";

    private schedule sc  = new schedule();

	//BS 规则
	private List<BSData> bsds = new ArrayList<>();

	public void parseBSCfg(String cfg,int lineId){

		List<String> ids = StrUtil.split(cfg, ',');
		if(ids.size() < 5){
			System.out.println("strategy cfg error");
			return;
		}
		BSData bsd =new BSData();
		int idx = 0;
		bsd.setOpen(Convert.toFloat(ids.get(idx)));
		idx++;
		bsd.setClose(Convert.toFloat(ids.get(idx)));
		idx++;
		bsd.setHigh(Convert.toFloat(ids.get(idx)));
		idx++;
		bsd.setLow(Convert.toFloat(ids.get(idx)));
		idx++;
		bsd.setVolume(Convert.toFloat(ids.get(idx)));

		System.out.println("strategyA:"+bsd.toString());
		bsds.add(bsd);
	}

	
    public String TodyDate(){

        // 获得当前日期
        DateTime dt = DateTime.now();
        int n1  = dt.getField(DateField.YEAR);
        int n2  = dt.getField(DateField.MONTH)+1;
        String n2s=""+n2;
        int n3  = dt.getField(DateField.DAY_OF_MONTH);
        String n3s=""+n3;
        if(n2<10){
            n2s="0"+n2;
        }
        if(n3<10){
            n3s="0"+n3;
        }
        String date = n1+"-"+n2s+"-"+n3s;
        return date;
    }

    // 上一个交易日
    public String LastDate(){
        return "";
    }


	@Override
    public String version() {
		System.out.println(version);
		return version;
	}

	@Override
	public void init(List<String> stockIdList) {
		// TODO Auto-generated method stub
		// throw new UnsupportedOperationException("Unimplemented method 'init'");
		
		// 获取db中指定stockid的历史信息
		sc.init("strategyA",stockIdList,"", this);
		
		//读取本地配置*.setting
		String fileName = "strategy_a.setting"; // 同级目录下的文件
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
			int lineId = 0;
            while ((line = br.readLine()) != null) {
                // System.out.println(line);
				parseBSCfg(line,lineId);
				lineId++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub
		// throw new UnsupportedOperationException("Unimplemented method 'start'");
		// 开启定时任务，判断BS时机
		sc.start();

	}

	@Override
	public void end() {
		// TODO Auto-generated method stub
		// throw new UnsupportedOperationException("Unimplemented method 'end'");
		sc.stop();
	}

	@Override
	public int BS(List<QuoteData> qs) {
		// TODO Auto-generated method stub
		// throw new UnsupportedOperationException("Unimplemented method 'BS'");
		// for(QuoteData q:qs){
		//     System.out.println("BS:"+q.toString());
		// }
		int n =  BS.check(qs,bsds);
		if(n == 0){
			return n;
		}
		int idx = qs.size()-1;
		String out = "";
		if(n == 1){
			// System.out.println("BS Sign::Buy:"+qs.get(idx).getName()+",price:"+qs.get(idx).getClose());
			String template = "[BS Sign][Buy]{},stockId:{},name:{},price:{}";
			out = StrUtil.format(template,DateTime.now(), qs.get(idx).getName(),qs.get(idx).getCnName(),qs.get(idx).getClose());
			System.out.println(out);
			//更新backtest
			sc.log(qs.get(idx),1);
		}else if(n == 2){
			String template = "[BS Sign][Sell]{},stockId:{},name:{},price:{}";
			out = StrUtil.format(template,DateTime.now(), qs.get(idx).getName(),qs.get(idx).getCnName(),qs.get(idx).getClose());
			// System.out.println(out);
			//更新backtest
			sc.log(qs.get(idx),0);

		}else{
			// System.out.println("[BS Sign]:Null");
		}

		// 发邮件
		MailImpl m = new MailImpl();
		// String mailto="";
		// m.sendEmail(mailto,"[strategyA]",out);

		

		return n;
	}


}
